package com.ilink.groundservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ilink.groundservice.entity.PO.langflowApp.CampApp;
import com.ilink.groundservice.entity.VO.langflowApp.CampHomeworkStatus;
import com.ilink.groundservice.mapper.langflowApp.CampAppMapper;
import com.ilink.groundservice.service.CampAppService;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CampAppServiceImpl  implements CampAppService {

    @Autowired
    private CampAppMapper campAppMapper;

    @Override
    public IPage<CampApp> getCampAppByCondition(String camp, String keywords, String tag, int page, int size) {
        LambdaQueryWrapper<CampApp> queryWrapper = new LambdaQueryWrapper<>();
        //只有审核成功的会展示在广场
        queryWrapper.ne(CampApp::getCheckStatus, "审核未通过");
        // 没有补充说明的会展示
        queryWrapper.and(wrapper -> wrapper.isNull(CampApp::getCheckComment));
        // 必填字段保证第一期
        queryWrapper.eq(CampApp::getCampName, camp);
        // 阿里的不是智能体应用
        queryWrapper.ne(CampApp::getCompanyName, "阿里");

        // 关键词匹配，查询所有的varchar字段
        if (StringUtils.isNotBlank(keywords)) {
            queryWrapper.like(CampApp::getStudentName, keywords)
                .or()
                .like(CampApp::getCompanyName, keywords)
                .or()
                .like(CampApp::getAgentName, keywords)
                .or()
                .like(CampApp::getAgentUrl, keywords)
                .or()
                .like(CampApp::getAgentIntroduction, keywords)
                .or()
                .like(CampApp::getAgentTag, keywords);
        }

        if (StringUtils.isNotBlank(tag)) {
            queryWrapper.like(CampApp::getAgentTag, tag);
        }

        Page<CampApp> campAppPage = new Page<>(page, size);
        IPage<CampApp> resultPage = campAppMapper.selectPage(campAppPage, queryWrapper);

        // 在查询结果之后转换 agentTag 字段
        List<CampApp> campApps = resultPage.getRecords();
        for (CampApp campApp : campApps) {
            // 将 agentTag 字段转换为字符串数组
            if (campApp.getAgentTag() != null) {
                String[] tags = campApp.getAgentTag().split(",");
                campApp.setAgentTagArray(tags);  // 假设你添加了一个新的字段 agentTagArray
            }
        }

        return resultPage;
    }


    @Override
    public boolean insertCampApp(CampApp campApp) {
        return campAppMapper.insert(campApp) > 0;
    }

    @Override
    public List<CampHomeworkStatus> getSubmittedAssignments(String studentId, String camp) {
        // 如果训练营名称不是"第一期训练营"，直接返回空列表
        if (!"第一期训练营".equals(camp)) {
            return new ArrayList<>();  // 返回空的列表
        }

        LambdaQueryWrapper<CampApp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CampApp::getStudentId, studentId)
            .eq(CampApp::getCampName, camp);
        List<CampApp> campApps = campAppMapper.selectList(queryWrapper);

        // 预定义的公司名称列表
        List<String> companies = List.of("百度", "讯飞", "字节", "阿里");

        // 初始化返回的列表
        List<CampHomeworkStatus> result = new ArrayList<>(4);

        // 初始化4个CampHomeworkStatus对象，默认公司名称为对应公司，并且flag为false
        for (String company : companies) {
            CampHomeworkStatus campHomeworkStatus = new CampHomeworkStatus();
            campHomeworkStatus.setListCompanyName(company);
            campHomeworkStatus.setFlag(false);
            result.add(campHomeworkStatus);
        }

        // 遍历数据库中的campApps，匹配公司名
        for (CampApp campApp : campApps) {
            String companyName = campApp.getCompanyName();
            for (CampHomeworkStatus status : result) {
                // 比较公司名
                if (companyName.equals(status.getListCompanyName())) {
                    status.setFlag(true);  // 设置flag为true
                    status.setCustomPrimary(campApp.getCustomPrimary());
                    status.setStudentId(campApp.getStudentId());
                    status.setStudentName(campApp.getStudentName());
                    status.setCompanyName(companyName);
                    status.setCampName(campApp.getCampName());
                    status.setAgentName(campApp.getAgentName());
                    status.setAgentUrl(campApp.getAgentUrl());
                    status.setAgentIntroduction(campApp.getAgentIntroduction());
                    status.setAgentTag(campApp.getAgentTag());
                    status.setCheckStatus(campApp.getCheckStatus());
                    status.setCheckComment(campApp.getCheckComment());
                    break;  // 找到匹配的公司名，跳出循环
                }
            }
        }

        return result;  // 返回包含4个CampHomeworkStatus对象的结果列表
    }





}
